
#include <iostream>
using namespace std;

class A {
  int aa;
  friend A operator+(const A& lhs, const A& rhs);
public:
  A(){cout<<"A()"<<endl;}
  ~A(){cout<<"~A()"<<endl;}
  void set(int a){aa =a;}
  int get() const{return aa;}
};

A operator+(const A& lhs, const A& rhs)
{
  A result;
  result.set(lhs.get()+rhs.get());
  return result;
}

int main()
{
  A a0;
  A a1;
  a0.set(1);
  a1.set(2);
  cout<<"--------------"<<endl;
  A a3 = a0+a1;
  cout<<"--------------"<<endl;
  const A& a4 = a0+a1;
  //error: invalid initialization of non-const reference of type 'A&' from
  //a temporary of type 'A'
  cout<<"--------------"<<endl;
  A& a5 = a3;
  cout<<"--------------"<<endl;
  A a6 = a3;
  cout<<"--------------"<<endl;
  cout<<"a3.aa\t"<<a3.get()<<endl;
  cout<<"a4.aa\t"<<a4.get()<<endl;
  cout<<"return 0"<<endl;
  return 0;
}
